package com.pactera.base.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.base.commons.utils.CopyUtil;
import com.pactera.base.example.entity.User;
import com.pactera.base.example.mapper.UserMapper;
import com.pactera.base.example.param.UserPageParam;
import com.pactera.base.example.service.UserService;
import com.pactera.base.example.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangrui
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public IPage<UserVo> findPage(UserPageParam param) {
        Page<User> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper<>(CopyUtil.copy(param,new User()));
        if(StringUtils.isNotBlank(param.getUserName())){
            wrapper.likeRight("user_name",param.getUserName());
        }
        Page<User> userPage = this.page(page, wrapper);
        Page<UserVo> userVoPage = new Page<>();
        if(userPage.getSize() > 0){
            userVoPage.setTotal(userPage.getTotal());
            userVoPage.setSize(userPage.getSize());
            userVoPage.setRecords(CopyUtil.copyList(userPage.getRecords(),UserVo::new));
        }
        return userVoPage;
    }
}
